<?php
/*
Version 0.1
SEMF - Web Desiging Framework for Sport Event Managment
Copyright (C) 2006, SEMF Developers

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/
$semf_xml = "./conf/semf.xml";
$projects = array();

$user_token = $_GET["user_token"];
if(!isset($user_token))
{
	$user_token= $_POST["user_token"];
}
	
$relative_url = "core/install/install.php";
if(!file_exists($semf_xml))
{
	header('Cache-control: no-cache');
	//should install everything..
	header("Location: http://" . $_SERVER['HTTP_HOST']
                     . dirname($_SERVER['PHP_SELF'])
                     . "/" . $relative_url); /* Redirect browser */
}
else
{
	$doc = new DOMDocument();
	$doc->load($semf_xml);
	
	$default_project_home = NULL;
	
	if($doc == NULL || $doc->documentElement == NULL)
	{
		header("Location: http://" . $_SERVER['HTTP_HOST']
                     . dirname($_SERVER['PHP_SELF'])
                     . "/" . $relative_url); /* Redirect browser */
	}
	$project_elements = $doc->documentElement->childNodes;
	
	foreach($project_elements as $project_element)
	{
		$project_name = $project_element->getAttribute("name");
		$child_nodes = $project_element->childNodes;
		
		$the_project = array();
		$the_project["name"] = $project_name;
		
		foreach($child_nodes as $child_node)
		{
			if($child_node->tagName == "dir")
			{
				$project_dir = getTextNodeValue($child_node);
				$the_project["project_dir"] = $project_dir;
				$default_project_home = $project_dir;
			}
		}
		
		$project_doc = new DOMDocument();
		$project_xml = $project_dir."/"."project.xml";
		$project_doc->load($project_xml);
		
		if($project_doc == NULL || $project_doc->documentElement == NULL)
		{
			header("Location: http://" . $_SERVER['HTTP_HOST']
						 . dirname($_SERVER['PHP_SELF'])
						 . "/" . $relative_url); /* Redirect browser */
		}
		
		$child_nodes = $project_doc->documentElement->childNodes;
		foreach($child_nodes as $child_node)
		{
			if($child_node->tagName == "database")
			{
				$db_name = $child_node->getAttribute("name");
				$the_project["db_name"] = $db_name;
				
				$server_child_nodes = $child_node->childNodes;
				foreach($server_child_nodes as $server_child_node)
				{
					if($server_child_node->tagName == "password")
					{
						$db_password = getTextNodeValue($server_child_node);
						$the_project["db_password"] = $db_password;
					}
					elseif($server_child_node->tagName == "server")
					{
						$db_server = getTextNodeValue($server_child_node);
						$the_project["db_server"] = $db_server;
					}
				}
			}
		}
		$projects[count($projects)] = $the_project;
	}
	$project_dir = $default_project_home;
	$semf_root = "./";
	$from_url = "index.php";
	include("./core/lib/login_checker.php");
}

function getTextNodeValue($node)
{
	if($node->firstChild)
	{
		return $node->firstChild->nodeValue;
	}
	return "";
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<script>
SEMF = {};
SEMF.submitPage = function()
{
	document.forms[0].submit();
}
</script>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>Web Designing Framework for Sport Event Management(SEMF)</title>
<link href="lib/style.css" rel="stylesheet" type="text/css" />
<link rel="shortcut icon" href="icons/semf.ico" />
<style type="text/css">
<!--
#Layer1 {
	position:absolute;
	left:366px;
	top:342px;
	width:49px;
	height:25px;
	z-index:3;
}
-->
</style>
</head>
<body>
<div id="upbg"></div>
<div id="outer">
  <div id="header">
    <div id="headercontent">
      <h1>SEMF<sup>1.0</sup></h1>
      <h2>Web development framework </h2>
    </div>
  </div>
  <div id="headerpic"></div>
  <div id="content">
      <div id="normalcontent">
      <h3><strong>SEMF projects </strong> </h3>	
	  
      <div class="contentarea">
        <?php
$i = 0;
foreach($projects as $project)
{
	$form_id = "form".$i;
	$project_name = $project["name"];
	$project_dir = $project["project_dir"];
	
	echo "<form id='${form_id}' method='post' action='project_home/index.php'>
			<table>
			<tr>
				<td width='150px'>
					<a href=\"javascript:SEMF.submitPage('${form_id}')\">${project_name}</a>
				</td>
				<td width='150px'>
					<a href=\"core/lib/schema_generator.php?project_home=${project_dir}\">
						Generate Schema
					</a>
				</td>
				<td width='150px'>
					<a href=\"core/lib/pages_generator.php?project_home=${project_dir}\">
						Generate Pages
					</a>
				</td>
				<td width='150px'>
					<a href=\"plug_ins/index.php?project_home=${project_dir}\">
						Plug-ins
					</a>
				</td>
				<td width='30px'>
				</td>
				<td>
					
					  <img title=\"Logout\" width=\"25\" height=\"25\" style=\"cursor:pointer\"
						onclick=\"window.location.href='core/login.php'\"
						src='images/logout.png' />
				</td>
				<td>
					<a href=\"#\" onclick=\"window.location.href='core/login.php'\">
						Logout
					</a>
				</td>
			</tr>
			</table>
			
			<input type='hidden' name='project_name' value='${project_name}'/>
			<input type='hidden' name='project_dir' value='${project_dir}'/>
			<input type='hidden' name='user_token' value='${user_token}'/>
			<input type='hidden' name='user_token' value='${user_token}'/>
		  </form>";
	$i ++;
}
?>
      </div>
    </div>
  </div>
  <div id="footer">
    <div class="left">&copy; 2007 SEMF developers</div>
    <div class="right"></div>
  </div>
</div>
<div id="Layer1"></div>

</body>
</html>
